package com.curry.android.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.curry.android.R;

/**使用场景:呼叫客服
 * */
public class PhoneDialog extends Dialog {

	private Context context;
	private String title;
	private String message;
	private String buttonLeftText;
	private String buttonRightText;
	private LeftClickListenerInterface left;
	private RightClickListenerInterface right;

	public PhoneDialog(Context context, String title, String message,
					   String buttonLeftText, String buttonRightText) {
		super(context, R.style.NewUIAlertViewStyle);

		this.context = context;
		this.title = title;
		this.message = message;
		this.buttonLeftText = buttonLeftText;
		this.buttonRightText = buttonRightText;
	}

	public interface LeftClickListenerInterface {
		public void doLeft();

	}

	public interface RightClickListenerInterface {
		public void doRight();

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		inite();
	}

	public void inite() {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_layout1, null);
		setContentView(view);

		TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
		TextView tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);
		TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);

		if (StringUtil.isBlank(title)) {
			tvTitle.setVisibility(View.GONE);
		} else {
			tvTitle.setText(title);
		}


		tvMessage.setText(message);
		if (buttonLeftText == null) {
			tvLeft.setVisibility(View.GONE);
		}
		if (buttonRightText == null) {
			tvRight.setVisibility(View.GONE);
		}
		tvLeft.setText(buttonLeftText);
		tvRight.setText(buttonRightText);

		tvLeft.setOnClickListener(new LeftClickListener());
		tvRight.setOnClickListener(new RightClickListener());

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics();

		lp.width = (int) (d.widthPixels * 0.8);
		dialogWindow.setAttributes(lp);
	}

	public void setClicklistener(LeftClickListenerInterface left, RightClickListenerInterface right) {
		this.left = left;
		this.right = right;
	}

	private class LeftClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			left.doLeft();
		}
	}

	private class RightClickListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			right.doRight();
		}
	}
}
